<template>
	<view>
		<!-- 查询具体周课表 -->
		<view class="top">
			<view class="lastweek" @click="lastWeek()">
				←上一周
			</view>
			<view class="currentweek">
				第{{zc}}周
			</view>
			<view class="nextweek" @click="nextWeek()">
				下一周→
			</view>
		</view>
		<view class="center">
			<u-table>
				<!-- 日期与星期显示 -->
				<u-tr>
					<u-th></u-th>
					<u-th style="font-size: 12px;" v-for="item in date">{{item.date+item.rq.split("2021-")[1]}}</u-th>
				</u-tr>
				<!-- 课表名称数据 -->
				<u-tr v-for="(item,index) in dateCourse" :key="index">
					<u-td>{{(2*index+1)+`/`+(2*index+2)}}</u-td>
					<u-td style="font-size: 12px;" v-for="(items,ind) in item" :key="ind">
						{{items.kcmc+`${items.kcmc==''?'':`@`}`+items.jxcdmc}}</u-td>
				</u-tr>
			</u-table>

		</view>
	</view>
</template>

<script>
	const day = new Map([
		["7", "周日"],
		["1", "周一"],
		["2", "周二"],
		["3", "周三"],
		["4", "周四"],
		["5", "周五"],
		["6", "周六"],
	]);
	const classMap = new Map([
		["0", "0102"],
		["1", "0304"],
		["2", "0506"],
		["3", "0708"],
		["4", "0910"],
	])
	export default {
		data() {
			return {
				xnxqdm: 202101, //当前学期
				zc: '', // 默认课表当前周
				course: [], //存储 返回每周课表数据
				date: [], //存储 后台返回每周对应的日期 
				dateCourse: [], //重新拼接数据课程名称、节数、日期数据
				class: undefined, //节数
			}
		},
		onLoad() {
			this.classInit()
			console.log("课程表初始化空数据：", this.dateCourse)
			this.getCurrentWeek()
		},
		created() {


		},
		methods: {
			// 获取课表数据
			async getCourse() {
				const res = await this.$schoolRequest({
					url: '/xsgrkbcx!getKbRq.action',
					method: 'GET',
					data: {
						xnxqdm: this.xnxqdm,
						zc: this.zc
					},
					header: {
						// 'content-type':''
						'X-Requested-With': 'XMLHttpRequest',
						'Cookie': uni.getStorageSync("sessionid"),
					},
					success: (res) => {
						this.course = res.data[0]
						this.classInit()
						console.log("课程数据：", this.course)
						if (this.course.length != 0) {
							this.course.map(el => {
								for (let i in this.dateCourse) {
									if (el.jcdm == classMap.get(i.toString())) {
										this.dateCourse[i].map(item => {
											if (item.jcdm == el.jcdm && item.xq == el.xq) {
												item.kcmc = el.kcmc
												item.jxcdmc = el.jxcdmc
											}
										})
									}
								}

							})
						}
						console.log("有kcmc数据：", this.dateCourse)


						this.date = res.data[1]
						this.date.map(el => {
							let date = day.get(el.xqmc)
							el.date = date
						})
						//课表时间由小到大排序
						this.date = this.date.sort(function(a, b) {
							if (a.rq) {
								return a.rq > b.rq ? 1 : -1
							}
						})
						console.log("星期数据：", this.date)
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			// 获取当前周值
			getCurrentWeek() {
				var date1 = "2021-08-29";
				var date2 = new Date().toISOString().slice(0, 10);

				var time1 = Date.parse(date1);
				var time2 = Date.parse(date2);

				var daycount = (Math.abs(time2 - time1)) / 1000 / 60 / 60 / 24;
				var currentweek = Math.floor((daycount / 7) + 1);
				this.zc = currentweek

				this.getCourse()
			},
			// 下一周
			nextWeek() {
				this.zc = this.zc + 1
				if (this.zc > 20) {
					this.zc = 1
				}
				this.getCourse()
			},
			// 上一周
			lastWeek() {
				this.zc = this.zc - 1
				if (this.zc < 1) {
					this.zc = 20
				}
				this.getCourse()
			},
			//拼接课程表初始化数据
			classInit() {
				this.dateCourse = [];
				for (let i = 0; i < 5; i++) {
					let temp = [];
					this.class = i.toString();
					for (let j = 0; j < 7; j++) {
						temp.push({
							kcmc: "", //课程名称
							jxcdmc: "", //课室地点
							jcdm: classMap.get(this.class), //节数
							xq: j + 1, //星期
						})

					}
					temp.unshift(temp[6])
					temp.pop()


					this.dateCourse.push(temp)
				}
			}
		}
	}
</script>

<style lang="scss">
	.top {
		height: 100rpx;
		width: 100%;
	}

	.lastweek,
	.currentweek,
	.nextweek {
		width: 33.3%;
		height: 100%;
		float: left;
		text-align: center;
		line-height: 100rpx;
	}

	.lastweek,
	.nextweek {
		cursor: pointer;
	}

	.u-td {
		height: 200rpx;
		width: 100rpx;
		overflow: hidden;
		padding: 0;
		margin: 0;
		text-align: left;
	}
</style>
